function yerefresh(){
    location.reload(true);
}
function hide(){
    if($("#searchheader").css("display")=="none"){
        $("#searchheader").css("display","block");
        $("#hide").text("收起筛选")
    }else if($("#searchheader").css("display")=="block"){
        $("#searchheader").css("display","none");
        $("#hide").text("打开筛选")
    }
}
function gj(classify,name,sale,scname,status,auditstatus){
    var mark=$("#mark").val();
    layui.use('table', function () {
        var table=layui.table;
        table.reload('demo',{
            where:{
                classify:classify,
                name:name,
                sale:sale,
                scname:scname,
                status:status,
                auditstatus:auditstatus,
                mark:mark
            }
        })
    })
}
function advancedsearch(){
    layer.open({
        type: 2,
        title: '高级检索',
        content: "../coursepage/advancedsearch.html",
        area: ['80%', '80%'],
        // end:function(){
        //
        // }
    })
}
function aa(res1) {
    if(res1==0){
        return '女'
    }else if(res1==1){
        return '男'
    }
}
function auditprocess(id){
    localStorage.setItem("auditprocessid",id);
    layer.open({
        type: 2,
        title: '审核详情',
        content: "../courseaudit/auditprocess.html",
        area: ['80%', '80%'],
    })
}
window.name="upload";
function refresh1() {
    tableIn.reload("demo");
}
var tableIn=null;
var kcname=null;
var treenode11=null;
var title1=null;
var data1=null;
    layui.use(['table','dropdown','form','laydate'], function () {
        var form=layui.form
        var dropdown = layui.dropdown
        var table = layui.table;
        var laydate = layui.laydate;
        //监听开关按钮
        laydate.render({
            elem: '#czsj' //指定元素
        });
        table.on('tool(test)', function(obj){
            var data = obj.data;
            var layEvent = obj.event;
            var tr = obj.tr;
            if(layEvent === 'detail'){ //查看
                localStorage.setItem("managerselectid",data.id)
                //do somehing
                layer.open({
                    type: 2,
                    title: '查看用户信息',
                    content: "selectmanagertable.html",
                    area: ['80%', '80%'],
                })
            } else if(layEvent === 'ban'){
                localStorage.setItem("banid",data.id)
                //do somehing
                layer.open({
                    type: 2,
                    title: '账号封停',
                    content: "managerlistban.html",
                    area: ['80%', '80%'],
                    success: function (layero, index) {//弹出层成功弹出后的回调函数
                        // 父页面向子页面传参 -- iframe 层传数据
                        var body = layer.getChildFrame('body', index);//获取iframe页面
                        body.find('#name').val(obj.data.name);
                    }
                })

            } else if(layEvent === 'update'){ //编辑
                console.log(data);
                console.log(data.id)
                localStorage.setItem("selectid",data.id)
                layer.open({
                    type: 2,
                    title: '编辑',
                    content: "updatemanagertable.html",
                    area: ['80%', '80%'],
                })
            } else if(layEvent === 'permission'){
                localStorage.setItem("managerperid",data.id)
                layer.open({
                    type: 2,
                    title: '设置权限',
                    content: "setpermissions.html",
                    area: ['80%', '80%'],
                })
            }
        });

        dropdown.render({
            elem: '#demo1', //可绑定在任意元素中，此处以上述按钮为例
            data:data1
            ,id: 'demo1'
            //菜单被点击的事件
            ,click: function(obj){
                var mark=$("#mark").val();
                if(mark==""){
                    mark=0;
                }
                console.log(obj.title);
                title1=obj.title;
                table.reload('demo',{
                    where:{
                        classify:title1,
                        mark:mark
                    }
                })
            }
        });
        //监听开关按钮
        form.on('switch(stat)', function(obj){
            var id = $(this).attr('menuId');//为了获取开关所在行的信息，需要绑定上所在行的id
            console.log('id:' + id);
            var isManager = obj.elem.checked?"1":"0";
            console.log('isManager:' + isManager);
            $.ajax({
                type:'post',
                data: {id:id,state:isManager},
                url: '/advanced/updatestate',
                success:function(res){

                }
            })
        })

        //第一个实例
        tableIn = table.render({
            elem: '#demo'
            , height: 800
            , id: 'demo'
            , url: '/advanced/getbackofficetable' //数据接口
            , page: true //开启分页
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'},
                {field: 'id', title: '编号'},
                {field:'account',title:'用户账号'},
                {field: 'piano',title: '图片',templet:function(res){
                        return '<div><img src="'+res.piano+'" width="70px" height="70px"></a></div>';
                    }},
                {field: 'name', title: '用户昵称'},
                {field: 'sex', title: '性别',
                    templet: function (d) {return aa(d.sex)}},
                {field: 'department', title: '所属部门'},
                {field: 'state', title: '账号状态',
                    templet:function(res){
                        let menuId=res.id;
                        if (res.state == 0) {
                            return "<input type='checkbox' menuId='" + menuId + "' lay-filter='stat'  lay-skin='switch' lay-text='封停|正常'>";
                        }else if(res.state == 1){
                            return "<input type='checkbox' menuId='" + menuId + "' lay-filter='stat'  lay-skin='switch' lay-text='封停|正常' checked>";
                        }
                }},
                {title: '操作',width:150, toolbar: '#barDemo'}
            ]],
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.state, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.total, //解析数据长度
                    "data": res.data //解析数据列表
                };
            },
        });
        $("#selectmanager").click(function(){
            var account=$("#yhzh").val();
            var name=$("#yhxm").val();
            var department=$("#ssbm").val();
            var registtime=$("#registtime").val();
            table.reload('demo',{
                where:{
                    account:account,
                    name:name,
                    department:department,
                    zctime:registtime
                }
            })
        })
    });
    $(document).ready(function () {
        $.ajax({
            type: "get",
            url: "/intermediate/getbackofficetree",
            async: true,
            success: function (res) {
                console.log(res.menulists)
                zTreeObj = $.fn.zTree.init($("#treeDemo"), {
                    async: {
                        enable: true
                    },
                    edit: {
                        // enable: true,
                        drag: {
                            isMove: true, // 打开移动功能，鼠标左键点击后拖拽
                            prev: true,
                            autoOpenTime: 0
                        },
                        editNameSelectAll: true,
                        showRemoveBtn: true,
                        removeTitle: "删除节点",
                        renameTitle: "编辑节点名称",
                        showRenameBtn: true,
                    },
                    view: {
                        selectedMulti: false,
                        showIcon: true,// 显示图标
                        showLine: true,
                        // addHoverDom: addHoverDom
                        // removeHoverDom : removeHoverDom
                    },
                    data: {
                        simpleData: {
                            enable: true,
                            idKey: 'id',
                            pIdKey: 'pid',
                            rootPId: 0
                        },
                        key: {
                            name: "name"
                        }
                    },
                    check: {
                        enable: true,
                        chkStyle: "checkbox",
                        chkboxType: {"Y": "s", "N": "s"}
                    },
                    // callback: {
                    //     onRightClick: onRightClick
                    //
                    // }
                }, res.menulists); //初始化树
                zTreeObj.expandAll(true);   //true 节点全部展开、false节点收缩
            }
        });
    });
function refresh(){
    $.ajax({
        type: "get",
        url: "/intermediate/getbackofficetree",
        async: true,
        success: function (res) {
            a =  res.menulists;
            zTreeObj = $.fn.zTree.init($("#treeDemo"), {
                async: {
                    enable: true
                },
                edit: {
                    // enable: true,
                    drag: {
                        isMove: true, // 打开移动功能，鼠标左键点击后拖拽
                        prev: true,
                        autoOpenTime: 0
                    },
                    editNameSelectAll: true,
                    showRemoveBtn: true,
                    removeTitle: "删除节点",
                    renameTitle: "编辑节点名称",
                    showRenameBtn: true,
                },
                view: {
                    selectedMulti: false,
                    showIcon: true,// 显示图标
                    showLine: true,
                    // addHoverDom: addHoverDom
                    // removeHoverDom : removeHoverDom
                },
                data: {
                    simpleData: {
                        enable: true,
                        idKey: 'id',
                        pIdKey: 'pid',
                        rootPId: 0
                    },
                    key: {
                        name: "name"
                    }
                },
                check: {
                    enable: true,
                    chkStyle: "checkbox",
                    chkboxType: {"Y": "s", "N": "s"}
                },
                // callback: {
                //     onRightClick: onRightClick
                // }
            }, a); //初始化树
            zTreeObj.expandAll(true);   //true 节点全部展开、false节点收缩
        }
    });
}
function del(){
    var zTree = $.fn.zTree.getZTreeObj("treeDemo");
    hideRMenu();
    var id1=$("#ak").val();
    console.log(1);
    if (confirm("是否删除？")) {  // 询问是否删除，若删除，则利用Ajax 技术异步删除，同是返回json格式数据，告知前台是否删除成功！
        $.ajax({
            url: "/primary/del",
            dataType: 'json',
            type: 'post',
            data: {"id": id1},
            success: function (response) {
                console.log(treenode11);
                zTree.removeNode(treenode11);     // z-Tree 的api ，从视角上 删除
                alert(response.msg);
            },
            error: function (response) {
                alert(response.msg);
                return false;
            }
        });
    }
    return;
}
function edit(){
    var pid3=$("#fileNode_pid").val();
    var title1=$("#fileNode_pname").val();
    var id1=$("#ak").val();
    layer.open({
        type: 2,
        title: '修改节点',
        content: "treeedit.html",
        area: ['80%', '80%'],
        success:function(layero,index){
            $("#rMenu").css({ "visibility": "hidden"});
            let childFrame=layer.getChildFrame('body',index);
            childFrame.find('input[name="pid"]').val(pid3);
            childFrame.find('input[name="name"]').val(title1);
            childFrame.find('input[name="id"]').val(id1);

        }
    })
}
function addNode(){
    var id2=$("#ak").val();
    layer.open({
        type: 2,
        title: '添加节点',
        content: "treeadd.html",
        area: ['80%', '80%'],
        success:function(layero,index){
            $("#rMenu").css({ "visibility": "hidden"});
            let childFrame=layer.getChildFrame('body',index);
            childFrame.find('input[name="pid"]').val(id2);
        }
    })
}
// function onRightClick(event, treeId, treeNode) {
//     treenode11=treeNode;
//     // console.log(treeNode);
//     $("#fileNode_pid").val(treeNode.pid);
//     $("#fileNode_pname").val(treeNode.name);
//     $("#ak").val(treeNode.id);
//     if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
//         showRMenu("root", event.clientX, event.clientY);
//     } else if (treeNode && !treeNode.noR) {
//         showRMenu("node", event.clientX, event.clientY);
//     }
// };
// //显示右键菜单
// function showRMenu(type, x, y) {
//     $("#rMenu").show();
//     $("#rMenu").css({"top": y + "px", "left": x + "px", "visibility": "visible"}); //设置右键菜单的位置、可见
//     $("body").bind("mousedown", onBodyMouseDown);
// }
//隐藏右键菜单
function hideRMenu() {
    $("#rMenu").css({"visibility": "hidden"}); //设置右键菜单不可见
    $("body").unbind("mousedown", onBodyMouseDown);
}
//鼠标按下事件
function onBodyMouseDown(event) {
    if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length > 0)) {
        // $("#rMenu").hide();
        $("#rMenu").css({ "visibility": "hidden"});
    }
}
function resett(){
    $("#addGoodsForm")[0].reset();
}
